/*
 * MapLayers.java
 *
 * Created on September 27, 2007, 8:55 PM
 *
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.apps.rtemaster.gui.map;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;
import org.fpdev.core.Scenarios;
import org.fpdev.core.basenet.BLink;
import org.fpdev.core.basenet.LinkTypes;
import org.fpdev.apps.rtemaster.RouteMaster;
import org.fpdev.apps.rtemaster.gui.RMGUI;

/**
 *
 * @author demory
 */
public class MapLayers {

  private SortedMap<Integer, MapLayer> layers_;
  private SortedMap<Integer, List<Integer>> groups_;
  private List<BLink> drawnLinks_;
  public final static int GROUP_BACK = 1;
  public final static int GROUP_MID = 2;
  public final static int GROUP_FRONT = 3;
  public final static int LAYER_NONWALKABLE = 1;
  public final static int LAYER_LOCAL_STREETS = 2;
  public final static int LAYER_THOROUGHFARES = 3;
  public final static int LAYER_BIKEPED = 4;
  public final static int LAYER_TRANSIT = 5;
  public final static int LAYER_RAILROAD = 6;

  /** Creates a new instance of MapLayers */
  public MapLayers(RouteMaster av, RMGUI gui) {
    layers_ = new TreeMap<Integer, MapLayer>();
    groups_ = new TreeMap<Integer, List<Integer>>();
    newLayer(LAYER_NONWALKABLE, "Non-Walkable Streets", GROUP_BACK, av, gui);
    newLayer(LAYER_LOCAL_STREETS, "Local Streets", GROUP_MID, av, gui);
    newLayer(LAYER_THOROUGHFARES, "Thoroughfares", GROUP_MID, av, gui);
    newLayer(LAYER_BIKEPED, "Bike/Ped Facilities", GROUP_MID, av, gui);
    newLayer(LAYER_TRANSIT, "Transit Facilities", GROUP_FRONT, av, gui);
    newLayer(LAYER_RAILROAD, "Railroad Corridors", GROUP_FRONT, av, gui);
  }

  private void newLayer(int id, String name, int group, RouteMaster av, RMGUI gui) {
    MapLayer layer = new MapLayer(id, name, av, gui);
    layers_.put(id, layer);

    if (!groups_.containsKey(group)) {
      groups_.put(group, new LinkedList<Integer>());
    }
    groups_.get(group).add(id);
  }

  public void draw(MapCanvas canvas, boolean drawShpPts) {
    /*drawnLinks_ = new LinkedList<CLink>();
    Iterator<MapLayer> layers = layers_.values().iterator();
    while(layers.hasNext()) 
    drawnLinks_.addAll(layers.next().draw(canvas, drawShpPts));
     */
    //System.out.println("in draw");
    drawnLinks_ = new LinkedList<BLink>();
    Iterator<MapLayer> layers = layers_.values().iterator();
    while (layers.hasNext()) {
      drawnLinks_.addAll(layers.next().initVisible());
    }

    Iterator<List<Integer>> groups = groups_.values().iterator();
    while (groups.hasNext()) {
      //System.out.println("drawing group");
      List<Integer> group = groups.next();
      Iterator<Integer> layerIDs;

      layerIDs = group.iterator();
      while (layerIDs.hasNext()) {
        int id = layerIDs.next();
        //System.out.println(" bg "+id);
        layers_.get(id).drawBG(canvas);
      }

      layerIDs = group.iterator();
      while (layerIDs.hasNext()) {
        int id = layerIDs.next();
        //System.out.println(" fg "+id);
        layers_.get(id).drawFG(canvas, drawShpPts);
      }

    }

  /*layers = layers_.values().iterator();
  while(layers.hasNext()) layers.next().drawBG(canvas);
  layers = layers_.values().iterator();
  while(layers.hasNext()) layers.next().drawFG(canvas, drawShpPts);*/

  }

  public List<BLink> getDrawnLinks() {
    return drawnLinks_;
  }

  public MapLayer getLayer(int id) {
    return layers_.get(id);
  }

  public Iterator<MapLayer> getLayers() {
    return layers_.values().iterator();
  }

  public Iterator<String> getLayerNames() {
    List<String> names = new LinkedList<String>();
    Iterator<MapLayer> layers = layers_.values().iterator();
    while (layers.hasNext()) {
      names.add(layers.next().getName());
    }
    return names.iterator();
  }

  public void clearVisibleLinks(Scenarios scenarios) {
    Iterator<MapLayer> layers = layers_.values().iterator();
    while (layers.hasNext()) {
      layers.next().clearVisibleLinks(scenarios.getScenarios());
    }
  }

  public void classifyLink(BLink link, int majorCode) {
    int layerID = 0;
    switch (link.getType() / 10) {
      case LinkTypes.NONWALKABLE:
        layerID = LAYER_NONWALKABLE;
        break;
      case LinkTypes.MAJOR_TFARE:
        if (majorCode <= 2) {
          layerID = LAYER_THOROUGHFARES;
        }
        break;
      case LinkTypes.MINOR_TFARE:
        if (majorCode <= 1) {
          layerID = LAYER_THOROUGHFARES;
        }
        break;
      case LinkTypes.OTHER_STREET:
        if (majorCode <= 0) {
          layerID = LAYER_LOCAL_STREETS;
        }
        break;
      case LinkTypes.BIKE_PED:
        layerID = LAYER_BIKEPED;
        break;
      case LinkTypes.TRANSIT:
        layerID = LAYER_TRANSIT;
        break;
      case LinkTypes.RAIL:
        layerID = LAYER_RAILROAD;
        break;

    }
    if (layerID > 0) {
      getLayer(layerID).classifyLink(link);
    }
  }
}
